package com.poxz.controller;

import com.poxz.common.response.Result;
import com.poxz.common.response.ResultUtils;
import com.poxz.model.Order;
import com.poxz.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 订单控制层
 *
 * @author poxz
 */
@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    /**
     * 创建订单
     *
     * @param productName 商品名称
     * @param productPrice 商品价格
     * @return Result<Long> 订单ID
     */
    @PostMapping("/create")
    public Result<Long> createOrder(String productName, Double productPrice) {
        Long orderId = orderService.createOrder(productName, productPrice);
        return ResultUtils.success(orderId);
    }

    /**
     *  根据订单ID查询订单
     *
     *  @param id 订单ID
     *  @return Result<Order> 订单
     */
    @GetMapping("/{id}")
    public Result<Order> getOrderById(@PathVariable Long id) {
        Order order = orderService.getOrderById(id);
        return ResultUtils.success(order);
    }
}
